package com.fgr.controller;

import com.fgr.beans.Animal;
import com.fgr.beans.AnimalType;
import com.fgr.service.AnimalService;
import com.fgr.service.AnimalTypeService;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AnimalTypeController {
    @Autowired
    AnimalTypeService animalTypeService;

    @Autowired
    AnimalService animalService;

    @GetMapping("allAnimalParentType")
    public String allAnimalParentType(Model model,Integer tyParentId){
        List<AnimalType> animalParentTypes = animalTypeService.selectAllAnimalTypeByParentType(tyParentId);
        model.addAttribute("animalParentTypes",animalParentTypes);
        return "foreground/adopt";
    }

    @ResponseBody
    @RequestMapping("findAnimalTypeByParentType")
    public void findAnimalTypeByParentType(@RequestParam("tyParentId") Integer tyParentId, HttpSession session){
        List<AnimalType> animalTypes = animalTypeService.selectAllAnimalTypeByParentType(tyParentId);
        session.setAttribute("animalTypes",animalTypes);
        //当点击父类别时，不显示动物
        session.setAttribute("animals",null);
    }


    @GetMapping("allAnimalParentTypeBg")
    public String allAnimalParentTypeBg(Model model,Integer tyParentId){
        List<AnimalType> animalTypes = animalTypeService.selectAllAnimalTypeByParentType(tyParentId);
        model.addAttribute("allAnimalType",animalTypes);
        model.addAttribute("t",0);
        return "background/animal-species";
    }

    @GetMapping("allAnimalTypeBg2")
    public String allAnimalTypeBg2(Model model,Integer tda){
        if (tda==0){
            List<AnimalType> animalTypes = animalTypeService.selectAllAnimalChildType();
            model.addAttribute("allAnimalType",animalTypes);
            model.addAttribute("tda",tda);
            return "background/addanimal";
        }else {
            AnimalType animalType = animalTypeService.selectAnimalTypeById(tda);
            model.addAttribute("animalType",animalType);
            model.addAttribute("tda",tda);
            return "background/addanimal";
        }

    }


    @GetMapping("allAnimalTypeBg3")
    public String allAnimalTypeBg3(Integer id,Model model){
        List<AnimalType> animalTypes = animalTypeService.selectAllAnimalChildType();
        model.addAttribute("allAnimalType",animalTypes);
        Animal animal = animalService.selectAnimalById(id);
        model.addAttribute("animal",animal);
        return "background/modifyanimal";
    }

    @PostMapping("insertAnimalType")
    @ResponseBody
    public String insertAnimalType(AnimalType animalType){
        String s = animalTypeService.insertAnimalType(animalType);
        return s;
    }

    @GetMapping("addAnimalTypeBg")
    public String addAnimalTypeBg(Integer t,Model model){
        if (t==0){
            //在父页面
            List<AnimalType> ParentAnimalTypes = animalTypeService.selectAllAnimalTypeByParentType(0);
            model.addAttribute("ParentAnimalTypes",ParentAnimalTypes);
            model.addAttribute("t",t);
            return "background/addspecies";
        }else {
            //在子页面
            model.addAttribute("t",t);
            AnimalType animalType = animalTypeService.selectAnimalTypeById(t);
            model.addAttribute("parentAnimalType", animalType);
            return "background/addspecies";
        }

    }




    @PostMapping("deleteAnimalType")
    @ResponseBody
    public String deleteAnimalType(@RequestParam("ty_id") Integer ty_id,Integer tyParentId){
        if (tyParentId==0){
            //删除的是父类
            //查询父类下面有没有子类
            List<AnimalType> animalTypes = animalTypeService.selectAllAnimalTypeByParentType(ty_id);
            if (animalTypes.size()==0){
                //没有子类型
                //只删除父类自己
                Integer integer1 = animalTypeService.deleteAnimalType(ty_id);
                if (integer1>0){
                    return "SUCCESS";
                }else {
                    return "ERRO";
                }

            }else {
                //有子类型
                for (AnimalType animalType:animalTypes) {
                    //删除每个子类型下面的动物
                    String s = animalService.deleteAnimalByTypeId(animalType.getTyId());
                }
                //删除所有子类型
                Integer integer = animalTypeService.deleteAnimalTypeByParentId(ty_id);
                //删除父类
                Integer integer1 = animalTypeService.deleteAnimalType(ty_id);
                if (integer>0&&integer1>0){
                    return "SUCCESS";
                }else {
                    return "ERRO";
                }
            }
        }else {
            //删除的是子类
            //查询该子类下面有没有动物
            List<Animal> animals = animalService.selectAnimalByTypeId(ty_id);
            if (animals.size()==0){
                //删除的子类下面没有动物
                //删除子类自己
                Integer integer = animalTypeService.deleteAnimalType(ty_id);
                if (integer>0){
                    return "SUCCESS";
                }else{
                    return "ERRO";
                }

            }else {
                //删除的子类下面有动物
                //删除动物和子类自己
                String s = animalService.deleteAnimalByTypeId(ty_id);
                Integer integer = animalTypeService.deleteAnimalType(ty_id);
                if (s.equals("SUCCESS")&&integer>0){
                    return s;
                }else {
                    return "ERRO";
                }
            }
        }
    }


}
